// miniprogram/pages/audit/audit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected_page:1,
    check_pending:[],
    deal_list:[],
    deal_number:0,
    record_pending:[],
  },
  changepage1:function(){
    this.setData({
      selected_page:1
    })
    this.getorder()
  },
  changepage2:function(){
    this.setData({
      selected_page:2
    })
    this.getrecord()
  },
  getrecord:function(){
    var that=this;
    wx.cloud.callFunction({
      name:"order",
      data:{
        type:"checkHistory",
      },
      success(res){
        console.log(res)
        if (res.result.code==200){
        let data=res.result.data
        for (let i=0;i<data.length;i++){
          data[i].time=that.TransTime(data[i].timestamp);
          // data[i].time=new Date(data[i].timestamp).toLocaleString();
          if (data[i].state==0){
            data[i].state_text="通过";
          }else if (data[i].state==1){
            data[i].state_text="不通过"
          }else if (data[i].state==2){
            data[i].state_text="未审核"
          }else if (data[i].state==3){
            data[i].state_text="审核中";
          }else{
            data[i].state_text="发生错误";
          }
          
        }
        that.setData({
          record_pending:data,
          msg:res.result.msg
        })
        console.log(that.data.record_pending)
        }else{
          wx.showToast({
            icon:'none',
            title:res.result.msg
          })
        }
      },
      fail(res){console.log("调用云函数失败",res)}	
    })
  },
  agree:function(e){
    // console.log(e)
    var that=this;
    let id=e.currentTarget.dataset.id
    // console.log(id)
    let item={_id:this.data.check_pending[id]._id,state:0};
    console.log(item)
    wx.showModal({
      title:'确认同意该申请吗？',
      success(res){
        if (res.confirm){
          that.data.deal_list.push(item);
          console.log(that.data.deal_list)
          that.data.deal_number=that.data.deal_number+1;
          let left="check_pending["+id+"].hidden"
          that.setData({
              [left]:true
          })
          if (that.data.deal_number==that.data.check_pending.length){
            that.submit_list()
          }
        }
      }
    })
  },
  disagree:function(e){
    var that=this;
    let id=e.currentTarget.dataset.id
    let item={_id:this.data.check_pending[id]._id,state:1,fail_reason:""};
    wx.showModal({
     
      title:'确认拒绝该申请吗？',
      editable:true,
      placeholderText:'请输入拒绝理由',
      success(res){
        if (res.confirm){
          item.fail_reason=res.content;
          that.data.deal_list.push(item);
          console.log(that.data.deal_list)
          that.data.deal_number=that.data.deal_number+1;
          let left="check_pending["+id+"].hidden"
          that.setData({
              [left]:true
          })
          if (that.data.deal_number==that.data.check_pending.length){
            that.submit_list()
          }
        }
      }
    })
  },
  submit_list:function(){
    var that=this;
    wx.cloud.callFunction({
      name:"order",
      data:{
        type:"confirmOrder",
        data:that.data.deal_list
      },
      success(res){
        console.log(res)
        that.setData({
          deal_list:[],
          deal_number:0
        })
      },
      fail(res){
        wx.showModal({
          title:'审核结果提交失败，请重试',
          confirmText:"重试",
          success(r){
            if (r.confirm){
              that.submit_list()
            }
          }
        })
        console.log('云函数调用失败',res)}
    })
  },
  check_max_timestamp:function(){ //查询最大时间戳
    var that=this;
    wx.cloud.callFunction({
      name:"timeTable",
      data:{
        type:"getMaxTimeTable",
        },
      success(res)
      {
        // console.log(res)
        if (res.result.code==200){
          let max_timestamp=res.result.data.timestamp;
          let max_time=that.TransTime(max_timestamp);
          // let max_time=new Date(max_timestamp).toLocaleDateString();
          that.setData({
            max_time:max_time,
            max_time_msg:res.result.msg
          })
         
        //  console.log(that.data)
        }
      },
      fail(res)
      {
        wx.showToast({
           icon: 'none',
               title: '云函数调用失败',
        })
      }
    })
  },
  checkOrder:function(){  //检查是否还有待审核预约
    let that=this;
    wx.cloud.callFunction({
      name:"order",
      data:{
        type:"checkOrder",
      },
      success(res){
        console.log(res)
        if (res.result.code==200){
          let data=res.result.data
          for (let i=0;i<data.length;i++){
            data[i].time=new Date(data[i].timestamp).toLocaleString()
          }
          that.setData({
            check_pending:data,
            msg:res.result.msg
          })
        }
        wx.showToast({
          icon:'none',
          title: res.result.msg,
        })
      },
      fail(res){
        wx.showToast({
          icon:'error',
          title: '云函数调用失败',
        })
      }
    })
  },
  getorder:function(){ //获取待审核信息
    let that=this;
    wx.cloud.callFunction({
      name:"order",
      data:{
        type:"getOrder"
      },
      success(res){
        console.log(res)
        if (res.result.code==200){
        let data=res.result.data
        for (let i=0;i<data.length;i++){
          data[i].time=that.TransTime(data[i].timestamp)
          // data[i].time=new Date(data[i].timestamp).toLocaleString()
        }
        console.log(data)
        that.setData({
          check_pending:data,
          msg:res.result.msg
        })
        console.log(that.data.check_pending)
        }else{
          wx.showToast({
            icon:'none',
            title: res.result.msg,
          })
        }
      },
      fail(res){
        wx.showToast({
          icon:'error',
          title: '云函数调用失败',
        })
      }
    })
  },
  TransTime:function(timestamp){
    let time=new Date(timestamp)
    let year=time.getFullYear();
    let month=time.getMonth()+1;
    let date=time.getDate();
    let hours=time.getHours();
    let result=year+'/'+month+'/'+date+' '+hours+':'+'00';
    // console.log(result)
    return result
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    this.check_max_timestamp()
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getorder()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.submit_list()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.submit_list()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getorder()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})